package it.enea.xlab.firstcrud.crudmanager;

import it.enea.xlab.firstcrud.common.Profile;
import it.enea.xlab.firstcrud.entity.Supplier;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


/**
 * @author X-Lab
 * 
 * Wrapper remoto di CRUDManagerImpl locale
 */

@Stateless
@Interceptors({Profile.class})
public class CRUDManagerImpl implements CRUDManagerRemote {
	
	@PersistenceContext(unitName="CRUDPersistenceLayer")
	private EntityManager eM; 

	public Long createSupplier(Supplier supplier) {
		
		eM.persist(supplier);
		
		return supplier.getSupplierId();
	}

	
	public Supplier readSupplier(Long supplierId) {
	
		return eM.find(Supplier.class, supplierId);
	}

	
	/**
	 * restituisce false in caso di 
	 * IllegalArgumentException - if instance is not an entity or is a removed entity
	 * 
	 * restituisce null in caso di 
	 * IllegalStateException - if this EntityManager has been closed.
	 * TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
	 */
	public Boolean updateSupplier(Supplier supplier) {
		
		Boolean result = true;
		
		 try {
			 if ( (supplier == null) || (supplier.getSupplierId() == null) )
			 		result = false;

			 eM.merge(supplier);
		} catch (IllegalArgumentException e) {
			result = false;
		} catch (Exception e2) {
			result = null;
		}
		 
		 return result;
	}

	
	/**
	 * restituisce false in caso 
	 * la readSupplier torni null
	 * IllegalArgumentException - if instance is not an entity or is a removed entity
	 * 
	 * restituisce null in caso di 
	 * IllegalStateException - if this EntityManager has been closed.
	 * TransactionRequiredException - if invoked on a container-managed entity manager of type PersistenceContextType.TRANSACTION and there is no transaction.
	 */
	public Boolean deleteSupplier(Long supplierId) {
		
		Supplier s = this.readSupplier(supplierId);
		
		if (s == null)
			return false;
		
		try {
			eM.remove(s);
		} catch (IllegalArgumentException e) {
			return false;
		} catch (Exception e2) {
			return null;
		}
		
		return true;
	}
	

/*	
 * NON USATA
 * public Collection<Supplier> getSupplierRegister() {
		
		try {	
			Query q1 = eM.createQuery("SELECT s FROM Supplier s");
			
			Collection<Supplier> temp =  q1.getResultList(); 
			
			return temp;
			
		} finally {
			eM.close();
		}
	}*/
	
	
	public HashMap<Long, Supplier>  getRegister() {
		
		HashMap<Long, Supplier> register = new HashMap<Long, Supplier>();

		Query q1 = eM.createQuery("SELECT s FROM Supplier s");
		List<Supplier> results = q1.getResultList();

		Iterator<Supplier> resultsIterator = results.iterator();
		
		while( resultsIterator.hasNext() ) {
			Supplier singleSupplier = (Supplier) resultsIterator.next();
			register.put(singleSupplier.getSupplierId(), singleSupplier);
		}
		
		return register;
	}
	
}

